package swingPractice;
/**
 * @author p@!
 *
 */
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class PanelPractice extends JPanel implements Runnable{
//	initialize thread
	public Thread thread = new Thread(this);
//	fps - count timer with 0
//	fpsMax - max time the thread is runnable
	public int fps = 0;
	public int fpsMax = 1000000;
//	is Game Begin/Running 
	public boolean isGameStart = true;
//	present width and length parameters
	public static int myHeight;
	public static int myWidth;
	
//	Room declaration
	public static Room room;
	
//	Image array to import and set it to rectangles
	public static Image[] tileset_grass = new Image [100];
//	Image array to import and set it to rectangles
	public static Image[] tileset_attack = new Image [100];
	
	public GameFileManager gameFileManager;
			
	
	public PanelPractice (){
//		start the game with thread
		thread.start();
//		setBackground(Color.RED);
	}
	
	
//	never use a runnable thread inside paintComponent 
//	don't repaint in paintComponent - crash, out of memory
//	repaint() -\inside paitComponent - infinite looping inside paint component
	public void paintComponent(Graphics gp){
//		setBackground(Color.RED);
//		gp.drawRect(1, 2, 50, 50);
//		gp.fillRect(55, 55, 50, 50);
//		setBackground(Color.RED);

		
		if(isGameStart){
//			Returns the current width of this component - 1000
			myWidth = getWidth();
//			Returns the current height of this component - 700
			myHeight = getHeight();
			System.out.println("");
			System.out.println("myWidth:\t" + myWidth + "\n myHeight:\t" + myHeight);
			defineGame();
			isGameStart = false;
		}
		/*
		 * Clears the specified rectangle by filling it with the background 
		 * color of the current drawing surface. 
		 * This operation does not use the current paint mode.
		*/ 
		gp.clearRect(0, 0, getWidth(), getHeight());
//		Draw game arena to specify path and validate the same... :])
		room.drawArena(gp); 
		
	}
	
	public void run(){
		
//		Game loop - run a thread
//		while(true)
//			System.out.println("Thread running");
		
		while(true){
			if(!isGameStart){
				room.physic();
			}
			repaint();
//			System.out.println(fps);
//			fps++;
			try{
				thread.sleep(1);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
				
	}
	
	public void defineGame(){
		room = new Room();
		gameFileManager = new GameFileManager();
		for(int i=0; i<tileset_grass.length; i++){
			tileset_grass[i] = new ImageIcon("res/grass.jpg").getImage();
			tileset_grass[i] = createImage(new FilteredImageSource(tileset_grass[i].getSource(), 
					new CropImageFilter(0, 256*i, 256, 256)));
		}
		/*
		 * tileset_air/defense/tower initalization and decleration
		*/
		for(int i=0; i<tileset_attack.length; i++){
			tileset_attack[i] = new ImageIcon("res/road.jpg").getImage();
			tileset_attack[i] = createImage(new FilteredImageSource(tileset_attack[i].getSource(), 
					new CropImageFilter(0, 64*i, 64, 64)));
		}
		
		
		
//		Code to load  a saved game details.......--:> the tile details to textfile.pa1
		gameFileManager.loadSavedGame("game1.pa1");
		
	}

}
